﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace Hotel.QLPhong
{
    public partial class PhongDangThue : Form
    {
        private string maphong;
        private DateTime daythue;
        public PhongDangThue(string phong)
        {
            maphong = phong;
            InitializeComponent();
        }

        private SqlConnection con;
        private SqlDataAdapter da;

        private void connect()
        {
            string cn = "Data Source=.;Initial Catalog=Hotel;Integrated Security=True";

            try
            {
                con = new SqlConnection(cn);
                con.Open();
            }
            catch (Exception)
            {
                MessageBox.Show("Khong the ket noi co so du lieu", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private string getdata(string sql, int vt)
        {
            SqlDataReader rdr = null;
            try
            {
                SqlCommand cmd = new SqlCommand(sql, con);
                rdr = cmd.ExecuteReader();
                while (rdr.Read())
                {
                    return rdr[vt].ToString();
                }

            }
            catch (Exception)
            {
                MessageBox.Show("loi");
            }
            return "";
        }

        private long tongtienphong, tongtiendv=0;
        private void PhongDangThue_Load(object sender, EventArgs e)
        {
            connect();
            // thong tin khach hang
            SqlDataReader rdr = null;
            string sql = "SELECT * FROM KhachHang WHERE MaPhong = '" + maphong + "' ORDER BY MaPhong DESC";
            try
            {
                SqlCommand cmd = new SqlCommand(sql, con);
                rdr = cmd.ExecuteReader();
                while (rdr.Read())
                {
                    lbkhTen.Text = rdr[1].ToString();
                    lbkhGioitinh.Text = rdr[2].ToString();
                    lbkhNgaysinh.Text = Convert.ToDateTime(rdr[3]).ToString("dd/MM/yyyy");
                    lbkhCmnd.Text = rdr[4].ToString();
                    lbkhQuoctich.Text = rdr[5].ToString();
                    lbkhDiachi.Text = rdr[6].ToString();
                    lbkhSodt.Text = rdr[7].ToString();
                }

            }
            catch (Exception)
            {
                MessageBox.Show("loi");
            }
            // thong tin thue phong
            connect();
            string sqlngaythue = "SELECT * FROM PhongLuuTru WHERE MaPhong = '" + maphong + "' ORDER BY STT DESC";
            string ngaythue = getdata(sqlngaythue, 3); daythue = Convert.ToDateTime(ngaythue);
            lbpNgaythue.Text = Convert.ToDateTime(ngaythue).ToString("dd/MM/yyyy");
            long Days = (long)((TimeSpan)(DateTime.Now - daythue)).TotalDays;
            lbpSongayo.Text = Days.ToString();

            connect();
            string sqlgiaphong = "SELECT * FROM Phong WHERE MaPhong LIKE '" + maphong + "'";
            string giaphong = getdata(sqlgiaphong, 5);
            tongtienphong = Days * Convert.ToInt32(giaphong); // tinh tong tien thue phong
            lbptienphog.Text = string.Format("{0:0,0 VND}", tongtienphong);

            // dich vu da dung
            connect();
            da = new SqlDataAdapter("SELECT KhoDichVu.TenHang, DichVuPhong.SoLuong, DichVuPhong.NgaySuDung, DichVuPhong.TongTien FROM DichVuPhong,KhoDichVu WHERE DichVuPhong.MaPhong LIKE '" +
                maphong + "' AND DichVuPhong.NgaySuDung >= '" + daythue + "' AND DichVuPhong.MaHang = KhoDichVu.MaHang", con);
            DataTable tb = new DataTable();
            da.Fill(tb);
            dataGrvDv.DataSource = tb;
            // tinh tong tien dich vu da dung
            int sodv = tb.Rows.Count;
            int i = 0;
            while (i < sodv)
            {
                tongtiendv = tongtiendv + Convert.ToInt32(tb.Rows[i][3]);
                i++;
            }
            lbtongtiendv.Text = tongtiendv.ToString() + "  VND";
        }

        private void btncancel_Click(object sender, EventArgs e)
        {
            this.Dispose();
        }

        private void btnthanhtoan_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("\t THANH TOÁN CHO PHÒNG NÀY \n Sau khi nhấn \"OK\" sẽ đưa trang thái phòng về \"phòng trống\" ", "Xác nhận", MessageBoxButtons.OKCancel, MessageBoxIcon.Information)==DialogResult.OK)
            {
                connect();
                string sqlthanhtoan = "UPDATE Phong SET TinhTrang = '0' WHERE MaPhong = '" + maphong + "'";
                serverdata(sqlthanhtoan);
                this.Dispose();
            }
            
        }

        public void serverdata(string sql)
        {
            try
            {
                connect();
                SqlCommand command = new SqlCommand();
                command.Connection = con;
                command.CommandText = sql;
                command.ExecuteNonQuery();
            }
            catch (Exception e)
            {
                MessageBox.Show("Lỗi");
            }
        }

        private void btnxuatHD_Click(object sender, EventArgs e)
        {
            ReportHoaDonThanhToan frm = new ReportHoaDonThanhToan(lbkhTen.Text, lbkhNgaysinh.Text, lbkhGioitinh.Text, lbkhCmnd.Text, lbkhSodt.Text, lbkhQuoctich.Text, lbkhDiachi.Text, daythue.ToString(), maphong, tongtienphong.ToString(), tongtiendv.ToString(), lbpSongayo.Text);
            frm.Show();
        }

    }
}
